package cn.senjoeson.timedowner.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.senjoeson.timedowner.R;
import cn.senjoeson.timedowner.base.MyBaseAdapter;
import cn.senjoeson.timedowner.bean.HeartDetailBean;
import cn.senjoeson.timedowner.dao.DBConfig;
import cn.senjoeson.timedowner.dao.HeartDetailtDao;

/**
 * Created by senjoeson on 2017/12/27.
 */

public class HeartListActivity extends AppCompatActivity {
    @BindView(R.id.back)
    ImageView mBack;
    @BindView(R.id.create)
    ImageView mCreate;
    @BindView(R.id.heartlistcount)
    TextView mHeartlistcount;
    @BindView(R.id.listview)
    ListView mListview;
    @BindView(R.id.emptylayout)
    FrameLayout mEmptyLayout;
    private long mTitieid;
    public ArrayList<HeartDetailBean> mHeartDetailBeans;
    public MyBaseAdapter<HeartDetailBean> mHeartDetailBeanMyBaseAdapter;
    public HeartDetailtDao mHeartDetailBeanDao;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heartlist);
        ButterKnife.bind(this);


        initData();
        initListener();

    }

    private void initListener() {
        mListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                new AlertDialog.Builder(parent.getContext())
                        .setTitle("想要删除该心愿单么?")
                        .setMessage("你想要删除'" + mHeartDetailBeans.get(position).getContent() + "'这条心愿单么?")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                long contentid = mHeartDetailBeans.get(position).getContentid();
                                mHeartDetailBeanDao.delete(contentid);
                                mHeartDetailBeanMyBaseAdapter.deleteItem(position);
                            }
                        })
                        .create().show();
                return true;
            }
        });
    }

    private void initData() {
        mTitieid = getIntent().getLongExtra("titieid", 0);
        mHeartDetailBeanDao = new HeartDetailtDao(this, DBConfig.TABLE_NAME_2);

        List<HeartDetailBean> list = mHeartDetailBeanDao.selectAll();
        mHeartDetailBeans = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getTitleid()==mTitieid) {
                mHeartDetailBeans.add(list.get(i));
            }
        }
        if (mHeartDetailBeans.size() > 0) {
            mHeartlistcount.setText(mHeartDetailBeans.size() + "个心愿单");
            mEmptyLayout.setVisibility(View.GONE);
            mListview.setVisibility(View.VISIBLE);
        } else {
            mEmptyLayout.setVisibility(View.VISIBLE);
            mListview.setVisibility(View.GONE);
        }
        mHeartDetailBeanMyBaseAdapter = new MyBaseAdapter<HeartDetailBean>(mHeartDetailBeans) {

            @SuppressLint("ViewHolder")
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                convertView = View.inflate(parent.getContext(), R.layout.item, null);
                TextView textView = convertView.findViewById(R.id.item_tv);
                textView.setText(mDataSets.get(position).getContent());
                return convertView;
            }
        };
        mListview.setAdapter(mHeartDetailBeanMyBaseAdapter);
    }

    @OnClick({R.id.back, R.id.create})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                onBackPressed();
                break;
            case R.id.create:
                Intent intent = new Intent(HeartListActivity.this, WriteActivity.class);
                intent.putExtra("titleid",mTitieid);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initData();
    }
}
